package com.mythesis.bpel.ui.client.wrapper;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.widgetideas.graphics.client.Color;
import com.google.gwt.widgetideas.graphics.client.GWTCanvas;
import com.mythesis.bpel.ui.client.controls.BpelControl;
import com.mythesis.bpel.ui.client.wrapper.ControlWrapper.Boundary;

public class ContainerControlWrapper extends ControlWrapper {

	protected List<ControlWrapper> wrappers;
	protected Boundary containerOutline;
	 
	public Boundary getContainerOutline() {
		return containerOutline;
	}

	public void setContainerOutline(Boundary containerOutline) {
		this.containerOutline = containerOutline;
	}

	public ContainerControlWrapper(BpelControl control) {
		super(control);
		// TODO Auto-generated constructor stub
		this.wrappers = new ArrayList<ControlWrapper>();
		this.containerOutline = new Boundary();
	}
	
	@Override
	public void addControl(ControlWrapper wrapper) {
		// TODO Auto-generated method stub
		wrappers.add(wrapper);
	}

	@Override
	public void removeControl(ControlWrapper wrapper) {
		// TODO Auto-generated method stub
		wrappers.remove(wrapper);
	}

	@Override
	public void renderControl() {
		// TODO Auto-generated method stub
		calculateContainerOuline();
		calculateOutline();
		
		Boundary a = controlOutline;
		Boundary b = containerOutline;
		outlinePanel.setPixelSize(b.w, b.h + b.y);
		canvas.resize(b.w, b.h + b.y);
		canvas.setLineWidth(1);
		canvas.setStrokeStyle(Color.GREY);
		canvas.setLineJoin(GWTCanvas.ROUND);
		canvas.setLineCap(GWTCanvas.ROUND);
		canvas.beginPath();
		canvas.moveTo(a.x, a.y);
		canvas.lineTo(a.x + a.w, a.y);
		canvas.lineTo(a.x + a.w, a.y + a.h);
		canvas.lineTo(a.x, a.y + a.h);
		canvas.closePath();
		canvas.stroke();
		
		canvas.beginPath();
		canvas.moveTo(b.x+5, b.y);
		canvas.lineTo(b.x, b.y);
		canvas.lineTo(b.x, b.y + b.h);
		canvas.lineTo(b.w, b.y + b.h);
		canvas.lineTo(b.w, b.y);
		canvas.lineTo(b.w-5, b.y);
		canvas.stroke();

		outlinePanel.add(canvas, 0, 0);
		outlinePanel.add(this.getControl(), 10, 5);
		setWidget(outlinePanel);
	}

	@Override
	public Boundary getOuline() {
		// TODO Auto-generated method stub
		return containerOutline;
	}

	@Override
	public void calculateOutline() {
		// TODO Auto-generated method stub
		int h = this.getControl().getOffsetHeight();
		int w = this.getControl().getOffsetWidth();
		
		controlOutline.setX(5);
		controlOutline.setY(0);
		controlOutline.setWidth(w+10);
		controlOutline.setHeight(h+10);
	}
	
	public void calculateContainerOuline(){
		int h = this.getControl().getOffsetHeight();
		int w = this.getControl().getOffsetWidth();
		int _w = w+20;
		int _h = 2*(h+10);
		
		containerOutline.setX(0);
		containerOutline.setY(h/2);
		containerOutline.setWidth(_w);
		containerOutline.setHeight(_h);
	}

	@Override
	public void renderFocus() {
		// TODO Auto-generated method stub
		if(!focus){
			renderControl();
			Boundary r = getOuline();
			canvas.setLineWidth(1);
			canvas.setFillStyle(Color.BLACK);
			canvas.fillRect(r.x, r.y, 4, 4);
			canvas.fillRect(r.x, r.y + r.h-4, 4, 4);
			canvas.fillRect(r.x + r.w -4, r.y + r.h -4, 4, 4);
			canvas.fillRect(r.x + r.w -4, r.y, 4, 4);
		}
		else {
			canvas.clear();
			renderControl();
		}
		focus = !focus;
	}
}
